<?php
# echo __FILE__ . PHP_EOL;

// 多维超全局变量
global $_VAR, $_CONF, $_INC, $_TMP, $F;

// 别名
$_FILE = __FILE__;

// 变量赋值
$_VAR[$_FILE] = array();
$_CONF[$_FILE]['_VHOSTS'] = _include(__DIR__ . '/config/vhosts.php', '_CONF.$_FILE._VHOSTS');
$_CONF[$_FILE]['']['_VHOSTS'] = var_cache($_CONF[$_FILE]['_VHOSTS'], '_CONF.$_FILE._VHOSTS', $_FILE);

// 错误报告
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 调试信息
$_GET['phpinfo'] = isset($_GET['phpinfo']) ? (int) $_GET['phpinfo'] : null;
if (isset($_SERVER['PATH_INFO']) && preg_match('/^\/phpinfo(|\/[\d]*)$/i', $_SERVER['PATH_INFO'], $_VAR[$_FILE]['matches'])) {
    $_GET['phpinfo'] = (int) trim($_VAR[$_FILE]['matches'][1], '/');
    # print_r($_VAR[$_FILE]['matches']);
}
if (null !== $_GET['phpinfo']) {
    if (!is_string($_GET['phpinfo'])) {
        $_GET['phpinfo'] = $_GET['phpinfo'] ? : -1;
        phpinfo((int) $_GET['phpinfo']);
        # exit;
    }
}

// 自定义主机名
$http_host = parse_url('//' . $_SERVER['HTTP_HOST'], PHP_URL_HOST);
if (preg_match('/^\/php(|\/|\/.*)/i', $_SERVER['REQUEST_URI'])) {
    $http_host = 'ariesphp.loc.urlnk.com';
}
if (isset($_GET['http_host'])) {
    $http_host = $_GET['http_host'];
    // 不记住，临时预览
    if (!isset($_GET['setcookie'])) {
        setcookie('http_host', $http_host, time()+60*60*24*30, '/');
    }
} elseif (isset($_COOKIE['http_host'])) {
    $http_host = $_COOKIE['http_host'];
}

// 虚拟主机配置
defined('URL_HOST') ? : define('URL_HOST', $http_host);

// 虚拟主机配置规则规范化
$_VAR[$_FILE]['_HOSTS'] = array();
foreach ($_CONF[$_FILE]['_VHOSTS'] as $key => $value) {    
    if (is_numeric($key)) { // 前缀后缀分组
        foreach ($value as $prefix => $val) {
            foreach ($val as $suffix => $v) {
                foreach ($v as $k => $project) {
                    if (is_numeric($k)) {
                        $k = $project . '.loc.urlnk.com';
                    }
                    $file = $prefix . $project . $suffix;
                    # echo $file . PHP_EOL;
                    $_VAR[$_FILE]['_HOSTS'][$k] = $file;
                }
                
            }
        }
        $key = null;
        # print_r($value);exit;
    } elseif (preg_match('/\//', $key)) { // 正则
        if (preg_match($key, URL_HOST, $matches)) {
            $key = URL_HOST;
            # print_r($matches);exit;
        }
        # $key = preg_replace('/\/+/', '', $key);
    } elseif (preg_match('/\s+/', $key)) { // 空格分隔多个
        $keys = preg_split('/\s+/', $key);
        $key = array_shift($keys);
        foreach ($keys as $k => $v) {
            $_VAR[$_FILE]['_HOSTS'][$v] = $value;
        }
        # print_r($keys);exit;
    }

    if (null !== $key) {
        $_VAR[$_FILE]['_HOSTS'][$key] = $value;
    }
}

# $_VAR[$_FILE][''] = array();
$_VAR[$_FILE]['']['_HOSTS'] = var_cache($_VAR[$_FILE]['_HOSTS'], '_VAR.$_FILE._HOST', $_FILE);
# debug_var(0, 0, 1);
# echo _include($F = $_VAR[$_FILE]['_HOSTS'][URL_HOST], true); 
# exit;

// 包含入口文件
if (array_key_exists(URL_HOST, $_VAR[$_FILE]['_HOSTS']) && _include($F = $_VAR[$_FILE]['_HOSTS'][URL_HOST], true)) {
    #echo 'debug_eval';exit;
    # debug_var('', 0, 1, []);
    return debug_eval(true, '');
} else {
    # echo $F, PHP_EOL, $_FILE, PHP_EOL;
    # $F = ;
    # $included = ;
    return debug_eval(_include($_VAR[$_FILE]['_HOSTS'][''], true), '');
}
